//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DiscretizedOption.h"
using namespace Cephei::QL;
#include <gen/QL/DiscretizedAsset.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CDiscretizedOption::CDiscretizedOption (Cephei::QL::IDiscretizedAsset^ underlying, QL::Exercise::TypeEnum exerciseType, Cephei::Core::IVector<Double>^ exerciseTimes) : CDiscretizedAsset(CDiscretizedOption::typeid)
{
    CDiscretizedAsset^ _Cunderlying;
    try
    {
#ifdef HANDLE
        _phDiscretizedOption = NULL;
#endif
        _Cunderlying = safe_cast<CDiscretizedAsset^> (underlying);
        _Cunderlying->Lock();
        boost::shared_ptr<QuantLib::DiscretizedAsset>& _underlying = static_cast<boost::shared_ptr<QuantLib::DiscretizedAsset>&> (_Cunderlying->GetShared ()); 
        QuantLib::Exercise::Type _exerciseType = (QuantLib::Exercise::Type)exerciseType ;
        exerciseTimes->Lock();
        INativeVector<Double>^ _NCIexerciseTimes = exerciseTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseTimes = safe_cast<CDoubleVector^>(_NCIexerciseTimes);
        std::vector<QuantLib::Time>& _exerciseTimes = static_cast<std::vector<QuantLib::Time>&> (_NCexerciseTimes->GetReference ());
        _ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (new QuantLib::DiscretizedOption ( _underlying,  _exerciseType,  _exerciseTimes ));
        SetDiscretizedAsset (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cunderlying != nullptr) _Cunderlying->Unlock();
        if (exerciseTimes != nullptr) exerciseTimes->Unlock();    //not optional
    }
}
Cephei::QL::CDiscretizedOption::CDiscretizedOption (boost::shared_ptr<QuantLib::DiscretizedOption>& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedOption = NULL;
#endif
	_ppDiscretizedOption = &childNative;
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
}
Cephei::QL::CDiscretizedOption::CDiscretizedOption (QuantLib::DiscretizedOption& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedOption = NULL;
#endif
	_ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (&childNative);
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
    _DiscretizedOptionOwner = owner;
    _DiscretizedAssetOwner = owner;
}

Cephei::QL::CDiscretizedOption::CDiscretizedOption (CDiscretizedOption^ copy) : CDiscretizedAsset(CDiscretizedOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (copy->GetShared());
        _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
    }
}
Cephei::QL::CDiscretizedOption::CDiscretizedOption (PLATFORM::Type^ t) : CDiscretizedAsset(CDiscretizedOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedOption = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CDiscretizedOption::CDiscretizedOption (QuantLib::Handle<QuantLib::DiscretizedOption>& childNative, Object^ owner)  : CDiscretizedAsset(CDiscretizedOption::typeid)
{
	_phDiscretizedOption = &childNative;
	_ppDiscretizedOption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedOption>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
    _DiscretizedOptionOwner = owner;
}
Cephei::QL::CDiscretizedOption::CDiscretizedOption (QuantLib::Handle<QuantLib::DiscretizedOption> childNative)  : CDiscretizedAsset(CDiscretizedOption::typeid)
{
	_phDiscretizedOption = &childNative;
	_ppDiscretizedOption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedOption>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
}
#endif
#ifdef STRUCT
Cephei::QL::CDiscretizedOption::CDiscretizedOption (QuantLib::DiscretizedOption childNative)  : CDiscretizedAsset(CDiscretizedOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedOption = NULL;
#endif
	_ppDiscretizedOption = new boost::shared_ptr<QuantLib::DiscretizedOption> (new QuantLib::DiscretizedOption (childNative));
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedOption));
}
#endif

Cephei::QL::CDiscretizedOption::~CDiscretizedOption ()
{
    if (_ppDiscretizedOption != NULL)
    {
	    delete _ppDiscretizedOption;
        _ppDiscretizedOption = NULL;
    }
}
Cephei::QL::CDiscretizedOption::!CDiscretizedOption ()
{
    if (_ppDiscretizedOption != NULL)
    {
	    delete _ppDiscretizedOption;
    }
}
QuantLib::DiscretizedOption& Cephei::QL::CDiscretizedOption::GetReference ()
{
    if (_ppDiscretizedOption == NULL) throw REFNEW NativeNullException ();
	return **_ppDiscretizedOption;
}
boost::shared_ptr<QuantLib::DiscretizedOption>& Cephei::QL::CDiscretizedOption::GetShared ()
{
    if (_ppDiscretizedOption == NULL) throw REFNEW NativeNullException ();
	return *_ppDiscretizedOption;
}
QuantLib::DiscretizedOption* Cephei::QL::CDiscretizedOption::GetPointer ()
{
    if (_ppDiscretizedOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppDiscretizedOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedOption>& Cephei::QL::CDiscretizedOption::GetHandle ()
{
	if (_phDiscretizedOption == NULL)
	{
		_phDiscretizedOption = new Handle<QuantLib::DiscretizedOption> (*_ppDiscretizedOption);
	}
	return *_phDiscretizedOption;
}
#endif
bool Cephei::QL::CDiscretizedOption::HasNative () 
{
	return (_ppDiscretizedOption != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::CDiscretizedOption::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedOption)->mandatoryTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedOption^ Cephei::QL::CDiscretizedOption::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //a
    	(*_ppDiscretizedOption)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IDiscretizedOption^ Cephei::QL::CDiscretizedOption_Factory::Create (Cephei::QL::IDiscretizedAsset^ underlying, QL::Exercise::TypeEnum exerciseType, Cephei::Core::IVector<Double>^ exerciseTimes)
{
    return REFNEW CDiscretizedOption ( underlying,  exerciseType,  exerciseTimes);
}
